package com.zhuimeng.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Activate(group = {CommonConstants.CONSUMER})
public class TransportIPFilter implements Filter, HandlerInterceptor {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        RpcContext.getContext().setAttachment("url",IPContext.getContext().getClientIp());
        return invoker.invoke(invocation);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        IPContext.getContext().setClientIp(request.getRemoteAddr());
        return true;
    }
}
